package jp.jma.oraclecard;

import java.io.File;

import net.cardgame.orcalecard.utils.ConstantValue;
import uk.co.senab.bitmapcache.BitmapLruCache;
import android.app.Application;

import com.novoda.imageloader.core.ImageManager;
import com.novoda.imageloader.core.LoaderSettings;
import com.novoda.imageloader.core.LoaderSettings.SettingsBuilder;
import com.novoda.imageloader.core.cache.LruBitmapCache;

public class MyApplication extends Application {
	private BitmapLruCache mCache;
	private ImageManager imageManager;
	private static MyApplication mApplication;

	@Override
	public void onCreate() {
		super.onCreate();
		mApplication = this;
		File cacheLocation;
		cacheLocation = new File(ConstantValue.getBasePatchApp(this)
				+ "/Android-BitmapCache");
		if (!cacheLocation.exists())
			cacheLocation.mkdirs();
		BitmapLruCache.Builder builder = new BitmapLruCache.Builder(this);
		builder.setMemoryCacheEnabled(true)
				.setMemoryCacheMaxSizeUsingHeapSize();
		builder.setDiskCacheEnabled(true).setDiskCacheLocation(cacheLocation)
				.setDiskCacheMaxSize(200000000);
		mCache = builder.build();
		mySettingCache();
	}

	public static MyApplication getInstance() {
		return mApplication;
	}

	public BitmapLruCache getBitmapCache() {
		return mCache;
	}

	private void mySettingCache() {
		SettingsBuilder settingsBuilder = new SettingsBuilder();

		// You can force the urlConnection to disconnect after every call.
		// settingsBuilder.withDisconnectOnEveryCall(true);

		// We have different types of cache, check cache package for more info
		settingsBuilder.withCacheManager(new LruBitmapCache(this));
		settingsBuilder.withExpirationPeriod(40 * 7l * 24l * 3600l * 1000l);
		// You can set a specific read timeout
		settingsBuilder.withReadTimeout(30000);

		// You can set a specific connection timeout
		settingsBuilder.withConnectionTimeout(30000);

		// You can disable the multi-threading ability to download image
		settingsBuilder.withAsyncTasks(true);

		// You can set a specific directory for caching files on the sdcard
		settingsBuilder.withCacheDir(new File("/something"));
		// Setting this to false means that file cache will use the url without
		// the query part
		// for the generation of the hashname
		settingsBuilder.withEnableQueryInHashGeneration(false);

		LoaderSettings loaderSettings = settingsBuilder.build(this);
		imageManager = new ImageManager(this, loaderSettings);
	}

	/**
	 * Convenient method of access the imageLoader
	 */
	public ImageManager getImageLoader() {
		return imageManager;
	}
}
